import { defineEventHandler, createError } from "h3"
import { UserService } from "../../../services/userService"

export default defineEventHandler(async (event) => {
  const session = event?.context?.session
  if (!session) {
    throw createError({
      statusCode: 401,
      message: '未授权'
    })
  }
  console.log('session', session)
  const user = await UserService.findById(session?.id)
  if (!user) {
    throw createError({
      statusCode: 401,
      message: '未授权'
    })
  }
  
  return {
    id: user.id,
    email: user.email,
    name: user.name,
    avatar: user.avatar,
    createdAt: user.createdAt,
  }
})